package com.chushouya.manager.service.admin.impl;


import com.chushouya.common.support.Contexts;
import com.chushouya.manager.dao.entity.ArticleCategoryEntity;
import com.chushouya.manager.dao.entity.ArticleEntity;
import com.chushouya.manager.dao.repository.ArticleCategoryRepository;
import com.chushouya.manager.dao.repository.ArticleRepository;
//=====manager.dto.admin.article.*;
import com.chushouya.manager.dto.admin.article.*;
import com.chushouya.manager.dto.admin.article.*;
import com.chushouya.manager.service.admin.ArticleService;
import com.general.framework.core.lang.Beans;
import com.general.framework.core.lang.Dates;
import com.general.framework.core.lang.Numbers;
import com.general.framework.core.lang.Validate;
import com.general.framework.core.schema.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;


@Service
public class ArticleServiceImpl implements ArticleService {
    
    private static final Logger logger = LoggerFactory.getLogger(ArticleServiceImpl.class);

    @Resource
    private ArticleRepository articleRepository;

    @Resource
    private ArticleCategoryRepository articleCatRepository;


    private ArticleCategoryEntity getCategory(Long id) {
        Validate.isNull(id, "文章分类id不能为空");
        ArticleCategoryEntity entity = articleCatRepository.selectByPrimaryKey(id);
        Validate.isNull(entity, "文章分类不存在");
        return entity;
    }

    @Override
    public PageResult<ArticleListDTO> selectPage(ArticleQuery query) {
        PageResult<ArticleEntity> page = articleRepository.selectPage(query);
        PageResult<ArticleListDTO> result = PageResult.of(page, ArticleListDTO.class);
        return result;
    }


    @Override
    public void save(ArticleSaveDTO dto) {

        Validate.isFalse(articleCatRepository.existsWithPrimaryKey(dto.getCategoryId()), "文章分类不存在");
        ArticleCategoryEntity articleCategoryEntity = this.getCategory(dto.getCategoryId());
        ArticleEntity entity = Beans.copy(dto, ArticleEntity.class);
        entity.setCategoryName(articleCategoryEntity.getCategoryName());
        entity.setHandleUser(Contexts.getUserContext().getUserName());
        entity.setCreateTime(Dates.getTimeNow());
        entity.setUpdateTime(Dates.getTimeNow());
        articleRepository.insertSelective(entity);

    }

    @Override
    public void update(ArticleUpdateDTO dto) {

        ArticleEntity entity = selectByPrimaryKey(dto.getArticleId());
        if (!Objects.equals(dto.getCategoryId(), entity.getCategoryId())) {
            Validate.isFalse(articleCatRepository.existsWithPrimaryKey(dto.getCategoryId()), "文章分类不存在");
        }
        ArticleCategoryEntity articleCategoryEntity = this.getCategory(dto.getCategoryId());
        ArticleEntity update = Beans.copy(dto, ArticleEntity.class);
        update.setCategoryName(articleCategoryEntity.getCategoryName());
        update.setHandleUser(Contexts.getUserContext().getUserName());
        update.setUpdateTime(Dates.getTimeNow());
        articleRepository.updateByPrimaryKeySelective(update);


    }

    private ArticleEntity selectByPrimaryKey(Long id) {
        Validate.isNull(id, "文章id不能为空");
        ArticleEntity entity = articleRepository.selectByPrimaryKey(id);
        Validate.isNull(entity, "文章不存在");
        return entity;
    }

    @Override
    public ArticleDetailDTO detail(Long id) {
        ArticleEntity entity = selectByPrimaryKey(id);
        return Beans.copy(entity, ArticleDetailDTO.class);
    }

    @Override
    public void remove(Long id) {
        Validate.isNull(id, "id不存在");
        ArticleEntity update = new ArticleEntity();
        update.setArticleId(id);
        update.setDelFlag(Numbers.INTEGER_ONE);
        update.setUpdateTime(new Date());
        update.setHandleUser(Contexts.getUserContext().getUserName());
        articleRepository.updateByPrimaryKeySelective(update);

    }

    @Override
    public void switchStatus(Long id, Integer status) {
        Validate.isNull(id, "id不能为空");
        Validate.isNull(status, "状态不能为空");
        ArticleEntity entity = selectByPrimaryKey(id);
        if (Objects.equals(entity.getStatus(), status)) {
            return;
        }
        ArticleEntity update = new ArticleEntity();
        update.setArticleId(id);
        update.setStatus(status);
        update.setUpdateTime(new Date());
        update.setHandleUser(Contexts.getUserContext().getUserName());
        articleRepository.updateByPrimaryKeySelective(update);

    }

}
